<?php
/* 
* [前台会员管理控制器]
* @Author: Jerry[502443279@qq.com]
* @Date:   2014-06-29 18:18:31
* @Last Modified by:   wangyi
* @Last Modified time: 2014-07-07 11:32:36
*/
//执行此操作必须为登陆客户
class MemberControl extends ValidateUserControl{
	//模型数据
	private $model;

	public function __auto(){
		$this->model = K('User');
	}
	/**
	 * [$auto 自动完成]
	 * @var array
	 */
	public $auto = array(
			//1.字段名，2.方法名，3.种类 有可能是function,4.验证条件，5.验证时机
			array('username','_username','method',2,3),
			array('nickname','_nickname','method',2,3),
			array('password','_password','method',2,3),
		);
		//用户名属性
		private function _username(){
			//以EMAIL为用户名
			$username = Q('post.username');
			return $username;
		}

		//昵称属性
		private function _nickname(){
			$nickname = Q('post.nickname');
			return $nickname;
		}
		//密码属性
		private function _password(){

		}

	public function index(){
		$this->dis('member.html');
	}
	//设置TOKEN APPID SECRET
	public function set(){
		$this->dis('member-set.html');
	}
	//微信LIST
	public function weilist(){
		$weixinifo = K('Weiuserinfo')->get_weixinifo_db();	
//		p($weixinifo);
		$this->assign('weixinifo',$weixinifo);
		
		$this->dis('member-weixinnumberlist.html');
	}
	public function addweiinfo(){
		if(IS_POST){
			//图片上传的处理
			//指定上传目录为 hd，允许上传类型为 jpg,jpeg,png 允许上传大小为 50000 字节，添加水印，生成缩略图，生成缩略图大小为宽 300，高 80，以固定宽度 高度裁切的方式 生成缩略图
			$upload = new upload('weixin_ico',array('jpg','jpeg','png'),50000,0,0,array(300,80,3));
			$uplofiles=$upload->upload();
			if(is_array($uplofiles)){
				$weixin_ico = current($uplofiles);
				$weixin_ico = $weixin_ico['path'];
			}
			$token = md5(time('HIS'));
			$EncodingAESKey = md5(md5(time('HIS'))).substr(md5(md5(md5(time('HIS')))),5,11);
			$weixin_user_uid =$_SESSION[md5('index')]['uid'];
			$wei_number = htmlspecialchars($_POST['wei_number']);
		$weiDB = array(
			'wei_user_ico'=>$weixin_ico,
			'sr_id'=>htmlspecialchars($_POST['sr_id']),
			'wei_number'=>$wei_number,
			'weixin_type'=>htmlspecialchars($_POST['weixin_type']),
			'appid'=>htmlspecialchars($_POST['appid']),
			'secret'=>htmlspecialchars($_POST['secret']),
			'weixin_type='=>$_POST['weixin_type'],
			'token'=>$token,
			//响应微信发送的Token验证
			'weixin_url'=>"Index/Wechat/index/user=".$weixin_user_uid."&wei_number=".$wei_number,
			//43位
			'EncodingAESKey'=>$EncodingAESKey,
			'weixin_user_uid'=>$weixin_user_uid,
		
			);
			$result = K('Weiuserinfo')->add_($weiDB);
			if($result){
				$this->success('成功添加','Member/weilist');
			}else{
				$this->error('添加有误','');
			}
		}
		$this->dis('member-addweixinnumber.html');
	}
	//验证码
	private function code(){
		$code = new Code();
		$code->show();
	}
	//删除微信列表数据
	public function ajax_del_weilist(){
		if(IS_AJAX){
			$result = K('Weiuserinfo')->del_weixinlist($_POST['id']);
			if($result){
				echo 1;
				die;
			}else{
				echo 0;
				die;
			}
					}
	}
	//检测用户名是否存在
	public function ajax_check_user(){
		if(!IS_AJAX) return false;
		$username = Q('post.username_reg');

		if(K('User')->check_user($username)){
			echo 0;die;
		}
		echo 1;die;
	}

	public function ajax_reg(){
		if(!IS_AJAX) return false;
		$data = array(
			'username' => Q('post.username_reg','','htmlspecialchars'),
			'nickname' => Q('post.nickname','','htmlspecialchars'),
			'password' => Q('post.password_reg1','','md5')
			);
		$uid = $this->model->add($data);
		if($uid){
			session('uid',$uid);
			session('username',$data['username']);
			//返回数据给AJAX
			$this->ajax($data,'json');
		}
	}

	public function out(){
		if (!IS_AJAX) return false;

		if ($_POST['out'] == 'yes') {
			session('username',NULL);
			session('uid',NULL);
			echo 1;die;
		}
		echo 0;die;
	}

	public function ajax_user_login(){
		if (!IS_AJAX) return false;
		$data = Q('post.');
		$data['password'] = md5($data['password']);
		$data = $this->model->get_one($data);
		if($data){
			session('uid',$data['uid']);
			session('username',$data['username']);
			//返回数据给AJAX
			$this->ajax($data,'json');
		}
		echo 0;die;
	}

	/**
	 * [ajax_check_code ajax检测验证码]
	 * @return [type] [description]
	 */
	public function ajax_check_code(){
		if(!IS_AJAX) return false;
		$code = Q('post.code','','strtoupper');
		if ($code == session('code')) {
			echo 1;die;
		}
		echo 0;die;
	}

}
